package com.dodayum.annotations;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class TimedAspect {

    @Pointcut("@within(com.dodayum.annotations.Timed)") // 切点：拦截带有 @Timed 注解的类
    public void beanAnnotatedWithTimed() {}

    @Around("beanAnnotatedWithTimed()")
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed(); // 执行被拦截的方法
        long endTime = System.currentTimeMillis();
        String methodName = joinPoint.getSignature().getName();
        long executionTime = endTime - startTime;
        String info = Arrays.toString(Thread.currentThread().getStackTrace());
        System.out.println("执行的方法名："+methodName);
        System.out.println("耗时："+executionTime);
        System.out.println("执行栈信息"+info);
        return result;
    }
}
